<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Supplier;
use App\Http\Controllers\App\BaseController;

class SupplierController extends BaseController
{   
    /**
     * 添加供货商
     *
     * @param Request $request
     * @return void
     */
    public function create( Request $request )
    {
        $name      = $request->input('name');
        $phone     = $request->input('phone');
        $link_name = $request->input('link_name');
        $link_tel  = $request->input('link_tel');
        $wechat    = $request->input('wechat');
        $qq        = $request->input('qq');
        $emali     = $request->input('emali');
        $province  = $request->input('province');
        $city      = $request->input('city');
        $area      = $request->input('area');
        $address   = $request->input('address');
        $note      = $request->input('note');

        $admin_id  = session('admin_id', 0);

        $code      = dechex(time());
        
        Supplier::create([
            'code'      => $code,
            'name'      => $name,
            'phone'     => $phone,
            'link_name' => $link_name,
            'link_tel'  => $link_tel,
            'wechat'    => $wechat,
            'qq'        => $qq,
            'emali'     => $emali,
            'province'  => $province,
            'city'      => $city,
            'area'      => $area,
            'address'   => $address,
            'note'      => $note,
            'admin_id'  => $admin_id,
        ]);

        return $this->success('添加成功');

    }

    /**
     *  查看供货商列表
     *
     * @param Request $request
     * @return void
     */
    public function show( Request $request )
    {
        $name = $request->input('name');
        $code = $request->input('code');

        $where = [];

        if ( $name ) {
            $where[] = [ 'name', 'like', '%'.$name.'%'];
        }

        if ( $code ) {
            $where[] = [ 'code', '=', $code ];
        }
       
        $data = Supplier::where($where)->paginate(15);

        return $this->json( $data, '查询成功');
    }

    /**
     * 更新供货商
     *
     * @return void
     */
    public function edit( Request $request )
    {   
        $id        = $request->input('id');
        $name      = $request->input('name');
        $phone     = $request->input('phone');
        $link_name = $request->input('link_name');
        $link_tel  = $request->input('link_tel');
        $wechat    = $request->input('wechat');
        $qq        = $request->input('qq');
        $emali     = $request->input('emali');
        $province  = $request->input('province');
        $city      = $request->input('city');
        $area      = $request->input('area');
        $address   = $request->input('address');
        $note      = $request->input('note');
        $status    = $request->input('status');

        $admin_id  = session('admin_id', 0);

        $info      = Supplier::find($id);
        if ( !$info ) {
            return $this->error('请传递正确的供货商');
        }
        Supplier::where('id', $id)->update([
            'name'      => $name,
            'phone'     => $phone,
            'link_name' => $link_name,
            'link_tel'  => $link_tel,
            'wechat'    => $wechat,
            'qq'        => $qq,
            'emali'     => $emali,
            'province'  => $province,
            'city'      => $city,
            'area'      => $area,
            'address'   => $address,
            'note'      => $note,
            'admin_id'  => $admin_id,
        ]);

        return $this->success('修改成功');
    }
}
